<template>
  <div class="todo-container">
    <div class="todo-wrap">

      <!-- 头部 -->
      <Header></Header>  <!-- 这里将对应的方法函数作为传入对象传递到子组件中 -->

      <!-- 列表部分 -->
      <List></List>

      <!-- 尾部 -->
      <Footer></Footer>

    </div>
  </div>
</template>


<script>
// 引入组件
import Header from './components/Header.vue';
import Footer from './components/Footer.vue';
import List from './components/List.vue';

import {provide, reactive} from 'vue';

export default {
  name: 'app',
  setup(props, context) {
    // 定义数据源
    let todos = reactive([
      {title: '打一场羽毛球', finished: false},
      {title: '打一场篮球', finished: false},
      {title: '打一场足球', finished: false},
      {title: '读一会书', finished: false},
    ]);

    // 根据索引删除数据
    const delTodoWithIndex = (index) => {
      debugger
      todos.splice(index, 1);
    };

    // 添加 todo
    const addTodo = (todo) => {
      todos.unshift(todo);
    };

    // 全选或者取消全选
    const selectedAllTodo = (isChecked) => {
      todos.forEach(todo => {
        todo.finished = isChecked;
      });
    };

    // 删除已完成的 todo
    const delFinishedTodos = () => {
      // 反着过来删除
      for(let i = todos.length - 1; i>= 0; i-- ){
        if(todos[i].finished){
          todos.splice(i, 1);
        }
      }
    };

    provide('todos', todos);
    provide('delTodoWithIndex', delTodoWithIndex);
    provide('addTodo', addTodo);
    provide('selectedAllTodo', selectedAllTodo);
    provide('delFinishedTodos', delFinishedTodos);

    return {
      todos, delTodoWithIndex, addTodo, selectedAllTodo, delFinishedTodos
    }
  },
  components: {
    Header,
    Footer,
    List
  },

}
</script>

<style>
body {
  background-color: #fff;
}

.btn {
  display: inline-block;
  padding: 8px 10px;
  margin-bottom: 0;
  font-size: 14px;
  line-height: 20px;
  text-align: center;
  vertical-align: middle;
  cursor: pointer;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.2), 0 1px 2px rgba(0, 0, 0, 0.5);
  border-radius: 4px;
}

.btn-warning {
  color: #fff;
  background-color: orange;
  border: none;
}

.btn-warning:hover {
  color: #fff;
  background-color: red;
}

.btn:focus {
  outline: none;
}

/*app*/
.todo-container {
  width: 600px;
  margin: 0 auto;
}

.todo-container .todo-wrap {
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 5px;
}
</style>
